package com.kyhd.audio.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@ComponentScan("com.kyhd.audio.redis")
public class RedisConfiguration {
	
	@Value("${redis.ip}")
    private String redis_ip;
	
	@Value("${redis.port}")
    private int redis_port;
	
	int timeout = 20000;
	
	@Bean
	JedisConnectionFactory jedisConnectionFactory() {
	    JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
	    jedisConFactory.setHostName(redis_ip);
	    jedisConFactory.setPort(redis_port);
	    jedisConFactory.setTimeout(timeout);
	    log.info("redis ip:{} port:{}",redis_ip,redis_port);
	    jedisConFactory.setDatabase(6);
	    return jedisConFactory;
	}
	
	
	@Bean(name="redisTemplate")
	public RedisTemplate<String, Object> redisTemplate() {
	    RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
	    template.setConnectionFactory(jedisConnectionFactory());
	    
	    template.setKeySerializer(new StringRedisSerializer());
	    template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
	    return template;
	}
}
